package mysql

import (
	"gorm.io/gorm"
	"wms/models"
)

// GetRoleBasicByName 根据 name 获取 role_basic
func GetRoleBasicByName(name string) *gorm.DB {
	tx := DB.Model(new(models.RoleBasic)).Select("*").
		Where("name = ?", name)
	return tx
}

// GetRoleBasicByNameWithoutIdentity 根据 name 获取 role_basic，但是 identity 不等于当前角色
func GetRoleBasicByNameWithoutIdentity(identity string, name string) *gorm.DB {
	tx := DB.Model(new(models.RoleBasic)).Select("*").
		Where("identity != ? AND name = ?", identity, name)
	return tx
}

// GetRoleBasicByIdentity 获取 role_basic 通过 identity
func GetRoleBasicByIdentity(identity string) *gorm.DB {
	tx := DB.Model(new(models.RoleBasic)).Select("*").
		Where("identity = ?", identity)
	return tx
}

// GetRoleBasicIdByIdentity 根据 identity 获取 role_basic 的原生 ID
func GetRoleBasicIdByIdentity(identity string) *gorm.DB {
	tx := GetRoleBasicByIdentity(identity).Select("id")
	return tx
}

// GetRoleListByKeyword 获取 role_basic 通过 keyword
func GetRoleListByKeyword(keyword string) *gorm.DB {
	tx := DB.Model(new(models.RoleBasic)).Select("identity, name, sort, is_admin, created_at, updated_at")
	if keyword != "" {
		tx.Where("name LIKE ?", "%"+keyword+"%")
	}
	tx.Order("sort ASC")
	return tx
}

// UpdateRoleAdminByIdentity 根据 identity 修改角色的管理员权限状态
func UpdateRoleAdminByIdentity(identity string, isAdmin int8) error {
	err := DB.Model(new(models.RoleBasic)).Where("identity = ?", identity).
		Updates(map[string]interface{}{
			"is_admin": isAdmin,
		}).Error
	return err
}

// TransactionCreateRole 事务下创建角色
func TransactionCreateRole(rb *models.RoleBasic, rm []*models.RoleMenu) error {
	err := DB.Transaction(func(tx *gorm.DB) error {
		err := tx.Create(rb).Error
		if err != nil {
			return err
		}
		// 填写 role_menu 结构中的 roleId
		for _, v := range rm {
			v.RoleId = rb.ID
		}
		// 菜单表不一定有，需要判断
		if len(rm) > 0 {
			err := tx.Create(rm).Error
			if err != nil {
				return err
			}
		}
		return nil
	})
	return err
}

// TransactionUpdateRole 事务下更新角色，role_basic, role_menu, role_function
func TransactionUpdateRole(rb *models.RoleBasic, rm []*models.RoleMenu, rf []*models.RoleFunction) error {
	err := DB.Transaction(func(tx *gorm.DB) error {
		var err error
		// 1. 更新 role_basic
		err = tx.Model(new(models.RoleBasic)).Where("identity = ?", rb.Identity).Updates(map[string]interface{}{
			"name":     rb.Name,
			"is_admin": rb.IsAdmin,
			"sort":     rb.Sort,
		}).Error
		if err != nil {
			return err
		}

		// 2. 删除 role_menu 老数据
		err = tx.Where("role_id = ?", rb.ID).Delete(new(models.RoleMenu)).Error
		if err != nil {
			return err
		}

		// 3. 创建新 role_menu
		if len(rm) > 0 {
			err = tx.Create(rm).Error
			if err != nil {
				return err
			}
		}

		// 4. 删除 role_function 老数据
		err = tx.Where("role_id = ?", rb.ID).Delete(new(models.RoleFunction)).Error
		if err != nil {
			return err
		}

		// 5. 创建新 role_function
		if len(rf) > 0 {
			err = tx.Create(rf).Error
			if err != nil {
				return err
			}
		}

		return nil
	})
	return err
}

// TransactionDeleteRole 事务删除角色及相关数据
func TransactionDeleteRole(identity string, roleId uint) error {
	err := DB.Transaction(func(tx *gorm.DB) error {
		// 1. 删除 role_basic
		err1 := tx.Where("identity = ?", identity).Delete(new(models.RoleBasic)).Error
		if err1 != nil {
			return err1
		}

		// 2. 删除 role_menu
		err2 := tx.Where("role_id = ?", roleId).Delete(new(models.RoleMenu)).Error
		if err2 != nil {
			return err2
		}

		// 3. 删除 role_function
		err3 := tx.Where("role_id = ?", roleId).Delete(new(models.RoleFunction)).Error
		if err3 != nil {
			return err3
		}

		return nil
	})
	return err
}
